<?php
/*
 * Created on
 * Date: 2007-12-4
 * Author: William Yang
 */
require_once(dirname(__FILE__)."/../common/constants.php");
require_once(DB_SESSION_DIR_PATH."/db_operator.php");
require_once(DB_SESSION_DIR_PATH."/db_obj_defs.php");
require_once(TRANSACTIONS_DIR_PATH."/trans_common.php");

class BulletinManager extends Bulletin {
	public function register() {
		$id = parent::register(CONFIGURATION_DIR_PATH."/create_bulletin.xml",
								FALSE);
		return $id;
	}
	
	public static function getTop($num) {
		$opera = new DatabaseOperator();
		$opera->config(CONFIGURATION_DIR_PATH."/bulletin.xml");
		
		$cList = new SqlCondList();
		$cList->add_constraint(new SqlConstraint(SqlConstraint::ORDERBY, "sent_at"));
		$cList->add_sort(new SqlSort(SqlSort::DESC));
		$cList->add_limit(new SqlLimit(0, $num));
		
		$rows = $opera->select($cList);
		
		$ret = array();
		
		$bulletin_delegate = new Bulletin();
		if ($rows != null && count($rows) > 0) {
			foreach ($rows as $row) {
				$tmp = $bulletin_delegate->array2copy($row);
				$ret[] = $tmp;
			}
		}
		
		return $ret;
	}
	
	public function query_by_uids($uid) {
		/*
		 * Query information by the UID specified by $uid.
		 */
		$rows = parent::query_by_uids(CONFIGURATION_DIR_PATH."/bulletin.xml", $uid);
		
		if ($rows === NULL) {
			return NULL;
		} else {
			$ret = array();
			$b_delegate = new Bulletin();
			foreach ($rows as $row) {
				$tmp = $b_delegate->array2copy($row);
				$ret[] = $tmp;
			}
			return $ret;
		}
	}
}
?>
